<?php

class NewsLetterController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
                /* init menu */
        require_once "models/Db/Event_db.php";
        require_once "models/Utility/Utility.php";

        $utility_handle = new Utility();
        $eventHandle    = new Event_db();

        $pivaList = $eventHandle->getAllPiva();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getUniqueNamebyPiva($piva);
                $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
        }
        $allDisco = $utility_handle->subval_sort($allDisco,0);
        $this->view->allDisco = $allDisco;

        foreach ($pivaList as $pivaList2){
            $piva = $pivaList2['piva'];
            $temp = $eventHandle->getPaeseDsicoByPiva($piva);
            $paese[$piva] = $temp['paese'];
            //$allDisco[] = $temp['name'];
            //$i++;
        }
        $this->view->paese = array_unique($paese);


        $categoryList = $eventHandle->getAllCategory();
        $this->view->category = $categoryList;

        $zonaList = array('Brescia','Verona','Mantova','Cremona');
        $this->view->zonaList = $zonaList;
    }

    public function getSubscribeForm(){
        require_once "models/Db/NewsLetter_db.php";
        //$users = new NewsLetter_db();
        //$this->view->users = $users->showAllUser();


        $form = new Zend_Form();

        $form->setAction('/newsletter/subscribe');
        $form->setMethod('post');
        $form->setDescription("Iscrizione news letter");
        $form->setAttrib('whichclub', 'newsletter');

        //name
        $form->addElement('text', 'name');
        $nomeElement = $form->getElement('name');
        $nomeElement->setLabel('Nome:');
        $nomeElement->setRequired(true);

        //Add validator
        $nomeElement->addValidator(
            new Zend_Validate_StringLength(4, 20)
            ); //Add Filter
        $nomeElement->addFilter(new Zend_Filter_StringToLower());
        $nomeElement->addFilter(new Zend_Filter_StripTags());

        //surname
        $form->addElement('text', 'surname');
        $cognomeElement = $form->getElement('surname');
        $cognomeElement->setLabel('Cognome:');

        //email address
        $form->addElement('text', 'email');
        $emailElement = $form->getElement('email');
        $emailElement->setLabel('E-mail:');
        $emailElement->setRequired(true);

        //Add Validator
        $emailElement->addValidator(
                new Zend_Validate_EmailAddress()
                );
        //Add Filter
        $emailElement->addFilter(new Zend_Filter_StripTags());

        //locali preferiti
        $nl_obj = new NewsLetter_db();
        $discoList = $nl_obj->getDisco();

        //metto in ordine alfabetico la lista dei locali
        require_once "models/Utility/Utility.php";
        $utility_handle = new Utility();
        $discoList = $utility_handle->subval_sort($discoList,'unique_name');

        foreach ($discoList as $discoList2):
            $form->addElement('checkbox', $discoList2['piva']);
            $preferitiElement=$form->getElement($discoList2['piva']);
            $preferitiElement->setLabel($discoList2['name']);
        endforeach;

        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Iscriviti');

        return $form;

    }
    public function indexAction()
    {
        $form = $this->getSubscribeForm();
        //Add the form to the view
        $this->view->form = $form;
    }

    public function getDeleteNewsLetterForm(){
         $form = new Zend_Form();

        $form->setAction('/newsletter/delete-success');
        $form->setMethod('post');
        $form->setDescription("Eliminazione dalla NewsLetter");

        //email address
        $form->addElement('text', 'email');
        $emailElement = $form->getElement('email');
        $emailElement->setLabel('E-mail:');
        $emailElement->setRequired(true);

        //Add Validator
        $emailElement->addValidator(
            new Zend_Validate_EmailAddress()
          );
        //Add Filter
        $emailElement->addFilter(new Zend_Filter_StripTags());

        //submit
        $form->addElement('submit', 'submit');
        $submitButton = $form->getElement('submit');
        $submitButton->setLabel('Cancellati!');

        return $form;
    }
    public function deleteAction()
    {
        // action body
        $form = $this->getDeleteNewsLetterForm();
        $this->view->form = $form;
    }

    public function subscribeAction()
    {
        // action body
        $form = $this->getSubscribeForm();

        //Check if the submitted data is POST type
        if($form->isValid($_POST)){
                
                require_once "models/Db/NewsLetter_db.php";

                //get info by the index.phtml view
                $name=$form->getValue("name");
                $surname=$form->getValue('surname');

                $nl_obj = new NewsLetter_db();
                $discoList = $nl_obj->getDisco();
                $preferiti="";
                foreach ($discoList as $discoList2):
                    if(($form->getValue($discoList2['piva']))==1){
                        $preferiti1 = ":" . $discoList2['piva'];
                    }else{
                        $preferiti1 ="";
                    }
                    $preferiti=$preferiti . $preferiti1;
                endforeach;

                $mail=$form->getValue("email");

                //send data to model
                $ins = new NewsLetter_db();
                $success=$ins->addUser($name, $surname, $mail, $preferiti);

                //send mail to the view
                $user=$mail;
                $this->view->user = $user;

                //send response to the subscribe.phtml view
                $this->view->success = $success;

            }else{
               $this->view->errors = $form->getMessages();
               $this->view->form = $form;
                }
    }

    public function deleteSuccessAction()
    {
        // action body
        $form = $this->getDeleteNewsLetterForm();

        //Check if the submitted data is POST type

        if($form->isValid($_POST)){
                require_once "models/Db/NewsLetter_db.php";

                $mail=$form->getValue("email");
                $ret = new NewsLetter_db();
                $success=$ret->deleteUser($mail);
                //$this->view->user = $user;
                $this->view->success = $success;
                //send data to model

            }else{
               $this->view->errors = $form->getMessages();
               $this->view->form = $form;
                }
    }

    public function sendEventAction()
    {
        // action body
        require_once "models/Db/NewsLetter_db.php";
        //$piva=2147483647;
        $nl_obj = new NewsLetter_db();
        $nl_obj->creacteMailNewsLetter();
//        $this->view->;
    }

}